package cn.mavor.day0910;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class ConsoleInputToFile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String fileName = "output.txt"; // 输出文件名
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); // 时间格式化器

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
            while (true) {
                System.out.print("请输入文字（输入 'quit' 退出）: ");
                String input = scanner.nextLine();

                if ("quit".equalsIgnoreCase(input)) {
                    break; // 退出循环
                }

                // 获取当前时间
                String currentTime = LocalTime.now().format(formatter);

                // 写入文件
                writer.write(currentTime + " " + input);
                writer.newLine(); // 换行
            }
        } catch (IOException e) {
            System.err.println("写入文件时发生错误: " + e.getMessage());
        }

        scanner.close();
        System.out.println("程序已退出。");
        System.out.println("当前工作目录: " + System.getProperty("user.dir"));
    }
}

